//
//  AEHomePageSubViewCell.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/18.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEHomePageSubViewCell.h"
#import "AE_Solar-Swift.h"
#import "SDCycleScrollView.h"
#import "AENewsInfoModel.h"

@interface AEHomePageSubViewCell () <SDCycleScrollViewDelegate>

@property (nonatomic, strong) AENewsInfoModel *model;

/// 分享按钮
@property (nonatomic, strong) TYZImageTitleButton *btnShare;

/// 标题
@property (nonatomic, strong) UILabel *titleLabel;

/// 日期图标
@property (nonatomic, strong) UIImageView *dateTagImgView;

/// 时间
@property (nonatomic, strong) UILabel *datetimeLabel;

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;

/// 图片 1/6
@property (nonatomic, strong) UILabel *imagePageLabel;

/// 点赞
@property (nonatomic, strong) TYZImageTitleButton *btnPraise;

/// 评论
@property (nonatomic, strong) TYZImageTitleButton *btnComment;


/// 日期图标
- (void)initWithDateTagImgView;

/// 时间
- (void)initWithDatetimeLabel;

@end

@implementation AEHomePageSubViewCell

- (void)initWithSubViewCell {
    [super initWithSubViewCell];
    
    self.contentView.backgroundColor = kWhiteColor;
    
    CGRect frame = CGRectMake(15, 0, kScreenW - 15 * 2, 1);
    [CALayer drawLine:self.contentView frame:frame lineColor:[UIColor colorWithHexString:@"#EBEBEB"]];
    
    [self initWithBtnShare];
}

/// 分享
- (void)initWithBtnShare {
    if (!_btnShare) {
        UIImage *image = [UIImage imageNamed:@"ic_main_share"];
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
        NSString *title = [AEStringExten localizedValueWithKey:@"分享"];
        float width = [title widthForFont:font] + 2 + 5 + 16 + image.size.width;
        CGRect frame = CGRectMake(kScreenW - width - 15, 17, width, 23);
        _btnShare = [TYZImageTitleButton createWithImageButton:self.contentView frame:frame font:font target:self action:@selector(actionWithShare:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kThemeColor forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:4];
            button.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
            button.layer.cornerRadius = frame.size.height * 0.5;
            button.layer.masksToBounds = YES;
        }];
    }
}

/// 标题
- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(15, 16, self.btnShare.left - 15 - 10, 22);
        _titleLabel = [UILabel createWithLabel:self.contentView frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#3D444D"];
//            label.text = @"二维若无若温热无若温热无若温热无若温热无若热污染我认为热污染温热无若";
        }];
    }
    if (self.model) {
        self.titleLabel.text = objectNull(self.model.newsTitle);
    }
}

/// 日期图标
- (void)initWithDateTagImgView {
    if (!_dateTagImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_main_date_tag"];
        CGRect frame = CGRectMake(15, _titleLabel.bottom + 6, image.size.width, image.size.height);
        _dateTagImgView = [UIImageView createWithImageView:self.contentView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
}

/// 时间
- (void)initWithDatetimeLabel {
    if (!_datetimeLabel) {
        CGRect frame = CGRectMake(self.dateTagImgView.right + 4, 0, kScreenW - self.dateTagImgView.right - 4 - 20, 16);
        _datetimeLabel = [UILabel createWithLabel:self.contentView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#8C9197"];
//            label.text = @"2024-10-12 23:34:23";
        }];
        _datetimeLabel.centerY = self.dateTagImgView.centerY;
    }
    if (self.model) {
        self.datetimeLabel.text = objectNull(self.model.newsDataTime);
    }
}

- (void)initWithCycleScrollView {
    if (!_cycleScrollView) {
        CGRect frame = CGRectMake(15, self.datetimeLabel.bottom + 10, kScreenW - 15 * 2, 150);
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:frame delegate:self placeholderImage:[UIImage imageNamed:@"placeholder_image"]];
        _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleScrollView.pageControlDotSize = CGSizeMake(5, 5);
        _cycleScrollView.currentPageDotColor = [UIColor clearColor];//[UIColor colorWithHexString:@"#D82626"];
        _cycleScrollView.pageDotColor = [UIColor clearColor];//[UIColor colorWithHexString:@"#000000"];
        _cycleScrollView.autoScrollTimeInterval = 6;
//        _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFit;
//        _cycleScrollView.backgroundColor = [UIColor orangeColor];
        _cycleScrollView.layer.cornerRadius = 6;
        _cycleScrollView.layer.masksToBounds = YES;
        _cycleScrollView.clipsToBounds = YES;
        [self.contentView addSubview:_cycleScrollView];
    }
    
    if (self.model) {
        self.cycleScrollView.imageURLStringsGroup = self.model.ossUrls;
    }
    
//    self.cycleScrollView.imageURLStringsGroup = @[@"https://nimg.ws.126.net/?url=http%3A%2F%2Fcms-bucket.ws.126.net%2F2024%2F1018%2Ff2bc3c47p00sljkek001qc000h400bbc.png&thumbnail=660x2147483647&quality=80&type=jpg", @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1018%2F0021af1bj00sliwt3003ed0010600m8m.jpg&thumbnail=660x2147483647&quality=80&type=jpg", @"https://nimg.ws.126.net/?url=http%3A%2F%2Fcms-bucket.ws.126.net%2F2024%2F1018%2F64b747baj00sljhxe00f9c000mv00csc.jpg&thumbnail=660x2147483647&quality=80&type=jpg"];
}

- (void)initWithImagePageLabel {
    if (!_imagePageLabel) {
        CGRect frame = CGRectMake(kScreenW - 30 - 60, self.btnShare.bottom + 40, 60, 23);
        _imagePageLabel = [UILabel createWithLabel:self.contentView frame:frame font:[UIFont systemFontOfSize:15 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
            label.backgroundColor = [UIColor colorWithHexString:@"#141317"];
            label.layer.cornerRadius = frame.size.height * 0.5;
            label.layer.masksToBounds = YES;
            label.textAlignment = NSTextAlignmentCenter;
            label.alpha = 0.8;
        }];
    }
    if (self.model) {
        if (self.model.ossUrls.count > 0) {
            _imagePageLabel.text = [NSString stringWithFormat:@"%d/%zd", 1, self.cycleScrollView.imageURLStringsGroup.count];
        } else {
            _imagePageLabel.text = [NSString stringWithFormat:@"%d/%zd", 0, self.cycleScrollView.imageURLStringsGroup.count];
        }
    }
}

// ic_main_praise_nor

/// 点赞
- (void)initWithBtnPraise {
    UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    if (!_btnPraise) {
        UIImage *image = [UIImage imageNamed:@"ic_main_praise_nor"];
        UIImage *imageSel = [UIImage imageNamed:@"ic_main_praise_sel"];
        NSString *str = @"0";
        float width = [str widthForFont:font] + 10 + image.size.width;
        CGRect frame = CGRectMake(self.cycleScrollView.right - 60 - width, self.cycleScrollView.bottom + 16, width, 20);
        _btnPraise = [TYZImageTitleButton createWithImageButton:self.contentView frame:frame font:font target:self action:@selector(actionWithPraise:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:str forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#3D444D"] forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button setImage:imageSel forState:UIControlStateSelected];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:8];
        }];
    }
    if (self.model) {
        NSString *str = [NSString stringWithFormat:@"%zd", self.model.newsLikes];
        [self.btnPraise setTitle:str forState:UIControlStateNormal];
        [self.btnPraise layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:8];
        self.btnPraise.selected = self.model.like;
    }
}

/// 显示评论数
- (void)initWithBtnComment {
    UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    if (!_btnComment) {
        UIImage *image = [UIImage imageNamed:@"ic_main_comment_nor"];
        UIImage *imageSel = [UIImage imageNamed:@"ic_main_comment_sel"];
        NSString *str = @"0";
        float width = [str widthForFont:font] + 10 + image.size.width;
        CGRect frame = CGRectMake(self.cycleScrollView.left + 60, self.cycleScrollView.bottom + 16, width, 20);
        _btnComment = [TYZImageTitleButton createWithImageButton:self.contentView frame:frame font:font target:self action:nil propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:str forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#3D444D"] forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button setImage:imageSel forState:UIControlStateSelected];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:8];
        }];
    }
    if (self.model) {
        NSString *str = [NSString stringWithFormat:@"%zd", self.model.newsCommentsCount];
        [self.btnComment setTitle:str forState:UIControlStateNormal];
        [self.btnComment layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:8];
    }
}

/// 点赞
/// - Parameter sender: sender description
- (void)actionWithPraise:(id)sender {
    if (self.touchPraiseBlock) {
        self.touchPraiseBlock();
    }
}

/// 分享
/// - Parameter sender: sender description
- (void)actionWithShare:(id)sender {
    if (self.touchShareBlock) {
        self.touchShareBlock();
    }
}


- (void)updateCellData:(id)cellEntity {
    
    self.model = cellEntity;
    
    [self initWithTitleLabel];
    
    /// 日期图标
    [self initWithDateTagImgView];

    /// 时间
    [self initWithDatetimeLabel];
    
    [self initWithCycleScrollView];
    
    [self initWithImagePageLabel];
    
    [self initWithBtnPraise]; // 点赞
    
    [self initWithBtnComment];

}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if (self.baseTableViewCellBlock) {
        self.baseTableViewCellBlock(nil);
    }
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
    _imagePageLabel.text = [NSString stringWithFormat:@"%zd/%zd", index, cycleScrollView.imageURLStringsGroup.count];
}



@end
